Le pouvoir de Jedi des Index dans l'univers de Postgres
CREATE INDEX [ name ] ON table_name
( { ( column_name [, ...] ) | ( expression ) }
[ ASC | DESC ]
[ NULLS { FIRST | LAST } ])
DROP INDEX name
[ CASCADE | RESTRICT ]
CREATE INDEX test1_id_index ON test1 (id)
DROP INDEX title_idx
CREATE INDEX test1_id_index ON test1 (id)
SELECT id FROM test1 WHERE id=5 ✅
SELECT id FROM test1 ❌
SELECT id FROM test1 WHERE fact(id)=120 ❌
SELECT * FROM mytable WHERE a = 5 AND b = 6
Attention à ORDER BY
CREATE INDEX ON mytable (a,b);
SELECT * FROM mytable WHERE a = 5 AND b = 6 ✅
SELECT * FROM mytable WHERE a = 5 ✅
SELECT * FROM mytable WHERE b = 6 ❌
CREATE INDEX ON mytable (lower(a))
SELECT * FROM mytable WHERE lower(a) = 'value' ✅
SELECT * FROM mytable WHERE a = upper('value') ❌
SELECT * FROM mytable WHERE a = 'value' ❌
CREATE INDEX ON mytable (a) WHERE a IS NULL
SELECT * FROM mytable WHERE a IS NULL ✅
SELECT * FROM mytable WHERE a = 5 ❌
CREATE INDEX ON mytable (a) INCLUDE (b)
SELECT a,b FROM mytable WHERE a = 5 ✅
SELECT a,b FROM mytable WHERE a = 5 AND b = 6 ❌
B-tree | Hash | BRIN | GIN | |
---|---|---|---|---|
= | ✅ | ✅ | ✅ | |
< <= >= > | ✅ | ✅ |
GiST | SP-GiST | GIN | |
---|---|---|---|
<@ >@ | ✅ | ✅ | ✅ |
&& | ✅ | ✅ | |
<< >> ~= <<| |>> | ✅ | ✅ | |
&< &> &<| |&> | ✅ |
CREATE INDEX name ON table (column ) USING method
CREATE INDEX pointloc ON points USING gist (box(location,location))
CREATE INDEX gin_idx ON documents_table USING GIN (locations)
CREATE INDEX name ON table (column opclass [ ( opclass_options ) ] )
Utilisé pour la recherche de pattern (régular expressions, sous-chaînes de caractères...)
CREATE INDEX name ON table (column)
[ COLLATE collation ]
[ ASC | DESC ]
[ NULLS { FIRST | LAST } ]
CREATE INDEX name ON table (column) [ NULLS [ NOT ] DISTINCT ]
ANALYZE
default_statistics_target
EXPLAIN [ ( option [, ...] ) ] statement
where option can be one of:
ANALYZE [ boolean ]
VERBOSE [ boolean ]
COSTS [ boolean ]
SETTINGS [ boolean ]
BUFFERS [ boolean ]
WAL [ boolean ]
TIMING [ boolean ]
SUMMARY [ boolean ]
FORMAT { TEXT | XML | JSON | YAML }
EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100;
QUERY PLAN
------------------------------------------------------------------------------
Bitmap Heap Scan on tenk1 (cost=5.07..229.20 rows=101 width=244)
Recheck Cond: (unique1 < 100)
-> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0)
Index Cond: (unique1 < 100)
\d pg_stat_all_indexes
View "pg_catalog.pg_stat_all_indexes"
Column | Type | Collation | Nullable | Default
---------------+--------------------------+-----------+----------+---------
relid | oid | | |
indexrelid | oid | | |
schemaname | name | | |
relname | name | | |
indexrelname | name | | |
idx_scan | bigint | | |
last_idx_scan | timestamp with time zone | | |
idx_tup_read | bigint | | |
idx_tup_fetch | bigint | | |